import axios from "axios";
import { getToken } from "./auth";
const instance = axios.create({
  baseURL: "http://lanlianhua.work:3009",
  // baseURL: "http://localhost:3009",
  timeout: 6000 /*请求超时时间*/,
});
// 全局请求拦截
instance.interceptors.request.use(
  function (config) {
    // 服务端使用www-form-urlencoded编码
    // config.data=qs.stringify(config.data)
    if (getToken()) {
      config.headers.authorization = `Bearer ${localStorage.getItem("token")}`;
    }
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);
//   全局响应拦截
instance.interceptors.response.use(
  function (response) {
    return response.data;
  },
  function (error) {
    return Promise.reject(error);
  }
);
//  get请求
export const get = (url, params) => instance.get(url, { params });
//  post请求
export const post = (url, data) => instance.post(url, data);
// delete请求
export const del = (url, params) => instance.delete(url, { params });
//  put请求
export const put = (url, data) => instance.put(url, data);
// 导出
export default instance;
